package com.excel.poi.util;

import com.excel.annotation.AnnotationUtil;
import com.excel.common.AnalyticalData;
import com.excel.common.Constant;
import com.excel.exception.ExcelException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.cglib.beans.BeanMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class PoiAnalyticalData extends AnalyticalData implements Constant {
    public PoiAnalyticalData() {
    }

    public static List<Object> analyticPoiExcel(Workbook book, Class<?> className) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        if (book == null) {
            throw new ExcelException("Workbook can not be empty!");
        } else {
            Map<String, Object> titleMap = AnnotationUtil.getTitleValueMap(className);
            if (titleMap != null && titleMap.size() != 0) {
                List<Object> allList = new ArrayList<Object>(1000);
                int sheetSize = book.getNumberOfSheets();
                if (sheetSize > 0) {
                    for(int i = 0; i < sheetSize; ++i) {
                        List<?> sheetList = analyticPoiExcel(book.getSheetAt(i), className, titleMap);
                        allList.addAll(sheetList);
                    }
                }

                return allList;
            } else {
                throw new ExcelException("Title does not contain ExcelAnnotation!");
            }
        }
    }

    public static List<Object> analyticPoiExcel(Sheet sheet, Class<?> className, Map<String, Object> titleMap) throws InstantiationException, IllegalAccessException {
        List<Object> list = new ArrayList<Object>(100);
        Row row = sheet.getRow(0);
        Map<Integer, Object> positionMap = new HashMap<Integer, Object>(10);
        Iterator<Cell> var7 = row.iterator();
        while(true) {
            Cell cell;
            do {
                if (!var7.hasNext()) {
                    for(int i = 1; i < sheet.getLastRowNum() + 1; ++i) {
                        Row rowCell = sheet.getRow(i);
                        if (rowCell != null) {
                            BeanMap beanMap = BeanMap.create(className.newInstance());
                            Iterator<Cell> var10 = rowCell.iterator();

                            while(var10.hasNext()) {
                                Cell cell1 = (Cell)var10.next();
                                String title = (String)positionMap.get(cell1.getColumnIndex());
                                if (title != null) {
                                    cell1.setCellType(1);
                                    String content = cell1.getStringCellValue();
                                    beanMap.put(title, getAttribute(className, title, content));
                                }
                            }

                            if (beanMap != null) {
                                list.add(beanMap.getBean());
                            }
                        }
                    }

                    return list;
                }

                cell = (Cell)var7.next();
            } while(cell == null);

            cell.setCellType(1);
            String cellValue = cell.getStringCellValue();
            if (cellValue == null || "".equals(cellValue)) {
                throw new ExcelException(sheet.getSheetName() + "Header line not empty cell!");
            }

            if (titleMap.get(cellValue) == null) {
                throw new ExcelException(className.getName() + "Object does not contain" + cellValue + "!");
            }

            positionMap.put(cell.getColumnIndex(), titleMap.get(cellValue));
        }
    }
}
